package com.whfc.fvs.service.impl;

import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.whfc.common.enums.DelFlag;
import com.whfc.common.exception.BizException;
import com.whfc.common.result.ListData;
import com.whfc.common.result.ResultEnum;
import com.whfc.common.util.RegexUtil;
import com.whfc.fvs.dao.FvsDeviceMapper;
import com.whfc.fvs.dao.FvsStreamMapper;
import com.whfc.fvs.dto.FvsDeviceDTO;
import com.whfc.fvs.dto.FvsRecordDTO;
import com.whfc.fvs.dto.FvsStreamDTO;
import com.whfc.fvs.dto.FvsTokenDTO;
import com.whfc.fvs.entity.FvsDevice;
import com.whfc.fvs.entity.FvsStream;
import com.whfc.fvs.enums.FvsDevicePtzOption;
import com.whfc.fvs.enums.FvsDeviceStatus;
import com.whfc.fvs.param.FvsDeviceAddParam;
import com.whfc.fvs.service.FvsPlatformService;
import com.whfc.fvs.service.factroy.FvsPlatformConstant;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Description: 直播地址播放
 * @author: qzexing
 * @version: 1.0
 * @date: 2020/4/30 13:48
 */
@Service(FvsPlatformConstant.LIVE_URL_PLAY_SERVICE)
public class FvsLiveUrlPlayServiceImpl implements FvsPlatformService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @NacosValue("${fvs.live.url.suffix.list}")
    private List<String> videoSuffixes;

    @Autowired
    private FvsStreamMapper fvsStreamMapper;

    @Autowired
    private FvsDeviceMapper fvsDeviceMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addDevice(FvsDeviceDTO fvsDeviceDTO, FvsDeviceAddParam deviceAddParam) {
        logger.info("LivePlayUrl 添加设备, deviceId:{}", fvsDeviceDTO.getId());
        FvsStream fvsStream = getFvsStream(deviceAddParam.getPlayUrl());
        FvsDevice fvsDevice = new FvsDevice();
        fvsDevice.setId(fvsDeviceDTO.getId());
        fvsDevice.setDeviceStatus(FvsDeviceStatus.ONLINE.getValue());
        fvsDeviceMapper.updateByPrimaryKeySelective(fvsDevice);
        fvsStream.setDeviceId(fvsDevice.getId());
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, 2099);
        fvsStream.setExpireTime(calendar.getTime());
        fvsStream.setDelFlag(DelFlag.UNDELETE.getValue());
        fvsStreamMapper.insertSelective(fvsStream);
    }


    @Override
    public FvsStreamDTO getStreamUrl(FvsDeviceDTO fvsDeviceDTO) {
        logger.info("LivePlayUrl 获取视频流地址, deviceId:{}", fvsDeviceDTO.getId());
        return fvsStreamMapper.selectByDeviceId(fvsDeviceDTO.getId());
    }

    @Override
    public void deleteDevice(FvsDeviceDTO fvsDeviceDTO) {
    }

    @Override
    public String getSnapshotUrl(FvsDeviceDTO fvsDeviceDTO) {
        return null;
    }

    @Override
    public ListData<FvsRecordDTO> getRecordList(FvsDeviceDTO fvsDeviceDTO, Date startTime, Date endTime) {
        return null;
    }

    @Override
    public void ptz(FvsDeviceDTO fvsDeviceDTO, FvsDevicePtzOption devicePtzOption) {
    }

    @Override
    public FvsTokenDTO getToken(Integer deptId) {
        return null;
    }

    /**
     * 跟直播地址获取流对象
     *
     * @param playUrl 播放地址
     * @return 流对象
     */
    private FvsStream getFvsStream(String playUrl) {
        logger.info("保存设备地址, playUrl:{}", playUrl);
        if (StringUtils.isEmpty(playUrl)) {
            throw new BizException(ResultEnum.PARAM_ERROR.getCode(), "直播地址不能为空.");
        }
        playUrl = StringUtils.lowerCase(playUrl).trim();
        FvsStream fvsStream = new FvsStream();
        //rtmp
        if (RegexUtil.isRtmp(playUrl)) {
            fvsStream.setRtmp(playUrl);
        }
        //hls
        else if (RegexUtil.isHls(playUrl)) {
            fvsStream.setHls(playUrl);
        }
        String suffix = playUrl.substring(playUrl.lastIndexOf(".") + 1);
        //配置格式
        if (videoSuffixes != null && !videoSuffixes.isEmpty() && videoSuffixes.contains(suffix)) {
            fvsStream.setPlayUrl(playUrl);
        }
        //其他
        else {
            throw new BizException(ResultEnum.PARAM_ERROR.getCode(), "直播地址格式错误.");
        }
        return fvsStream;
    }
}
